import 'package:flutter/cupertino.dart';
import 'package:netease_cloud_music_api/utils/crypto.dart';
import 'package:netease_cloud_music_api/utils/enum.dart';
import 'package:netease_cloud_music_api/utils/player_request.dart';
import 'package:netease_cloud_music_api/utils/request.dart';

mixin Artist {
  /// 歌手专辑列表
  /// [id] 歌手 id
  /// [limit] 取出数量 , 默认为 30
  /// [offset] 偏移数量 , 用于分页 , 如 :( 页数 -1)*30, 其中 30 为 limit 的值 , 默认 为 0
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> artistAlbum({int? limit, int? offset, required int id}) {
    Map<String, dynamic> data = {'limit': limit ?? 30, 'offset': offset ?? 0, 'total': true};
    return PlayerRequest(
        url: 'https://music.163.com/weapi/artist/albums/$id',
        data: data,
        options: PlayerRequestOptions(
          crypto: EncryptType.weapi,
          method: PlayerRequestMethod.post,
        )).request();
  }

  /// 歌手介绍
  /// [id] 歌手 id
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> artistDesc({required int id}) {
    Map<String, dynamic> data = {'id': id};
    return PlayerRequest(
        url: 'https://music.163.com/weapi/artist/introduction',
        data: data,
        options: PlayerRequestOptions(
          crypto: EncryptType.weapi,
          method: PlayerRequestMethod.post,
        )).request();
  }

  /// 歌手分类
  /// [artistArea] ALL: 全部, ZH: 华语, EA: 欧美, KR: 韩国, JP: 日本, Other: 其他
  /// [artistType] MaleSinger: 男歌手, FemaleSinger: 女歌手, Band: 乐队, ALL: 全部
  /// [limit] 取出数量 , 默认为 30
  /// [offset] 偏移数量 , 用于分页 , 如 :( 页数 -1)*30, 其中 30 为 limit 的值 , 默认 为 0
  /// [initial] 按首字母索引查找参数
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> artistList({ArtistArea? artistArea, ArtistType? artistType, int? limit, int? offset, String? initial}) {
    Map<String, dynamic> data = {'limit': limit ?? 30, 'offset': offset ?? 0, 'total': true};
    RegExp regExp = RegExp(r'[a-zA-Z]');
    if (initial != null && regExp.hasMatch(initial[0])) {
      data['initial'] = initial.toUpperCase().codeUnitAt(0);
    }
    if (artistArea == ArtistArea.ALL) data['area'] = -1;
    if (artistArea == ArtistArea.EA) data['area'] = 96;
    if (artistArea == ArtistArea.ZH) data['area'] = 7;
    if (artistArea == ArtistArea.KR) data['area'] = 16;
    if (artistArea == ArtistArea.JP) data['area'] = 8;
    if (artistArea == ArtistArea.Other) data['area'] = 0;

    if (artistType == ArtistType.ALL) data['type'] = -1;
    if (artistType == ArtistType.MaleSinger) data['type'] = 1;
    if (artistType == ArtistType.FemaleSinger) data['type'] = 2;
    if (artistType == ArtistType.Band) data['type'] = 3;

    return PlayerRequest(
      url: 'https://music.163.com/api/v1/artist/list',
      // data: data,
      options: PlayerRequestOptions(method: PlayerRequestMethod.post, crypto: EncryptType.weapi),
    ).request();
  }

  /// 歌手相关MV
  /// [id] 歌手 id, 可由搜索接口获得
  /// [limit] 取出数量 , 默认为 30
  /// [offset] 偏移数量 , 用于分页 , 如 :( 页数 -1)*30, 其中 30 为 limit 的值 , 默认 为 0
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> artistMv({required int id, int? limit, int? offset}) {
    Map<String, dynamic> data = {'limit': limit ?? 30, 'offset': offset ?? 0, 'total': true, 'artistId': id};
    return PlayerRequest(
      url: 'https://music.163.com/weapi/artist/mvs',
      data: data,
      options: PlayerRequestOptions(method: PlayerRequestMethod.post, crypto: EncryptType.weapi),
    ).request();
  }

  /// 歌手全部歌曲
  /// [id] 歌手id, 通过搜索可获得
  /// [order] 按照热门货时间排序
  /// [limit] 取出数量, 默认为30
  /// [offset]偏移数量,  用于分页 , 如 :( 页数 -1)*30, 其中 30 为 limit 的值 , 默认 为 0
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> artistSongs({required int id, ArtistOrder? order, int? limit, int? offset}) {
    Map<String, dynamic> data = {'id': id, 'private_cloud': 'true', 'work_type': 1, 'offset': offset ?? 0, 'limit': limit ?? 30, 'order': 'hot'};
    if (order == ArtistOrder.Hot) data['order'] = 'hot';
    if (order == ArtistOrder.Time) data['order'] = 'time';
    return PlayerRequest(
      url: 'https://music.163.com/api/v1/artist/songs',
      data: data,
      options: PlayerRequestOptions(method: PlayerRequestMethod.post, crypto: EncryptType.weapi, cookie: 'os=pc'),
    ).request();
  }

  /// 收藏与取消收藏歌手
  /// [collect] true 收藏, false 取消收藏
  /// [id] 歌手id， 通过搜索获取
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> artistSub({required bool collect, required int id}) {
    Map<String, dynamic> data = {'artistId': id, 'artistIds': '[$id]'};
    print(data);
    return PlayerRequest(
        url: 'https://music.163.com/weapi/artist/${collect ? 'sub' : 'unsub'}',
        data: data,
        options: PlayerRequestOptions(method: PlayerRequestMethod.post, crypto: EncryptType.weapi))
        .request();
  }

  /// 关注歌手列表
  /// [limit] 取出数量, 默认为30
  /// [offset]偏移数量,  用于分页 , 如 :( 页数 -1)*30, 其中 30 为 limit 的值 , 默认 为 0
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> artistSublist({int? limit, int? offset}) {
    Map<String, dynamic> data = {'total': true, 'limit': limit ?? 30, 'offset': offset ?? 0};
    return PlayerRequest(
      url: 'https://music.163.com/weapi/artist/sublist',
      data: data,
      options: PlayerRequestOptions(method: PlayerRequestMethod.post, crypto: EncryptType.weapi),
    ).request();
  }

  /// 歌手热门 50 首歌曲
  /// [id]  歌手id, 通过搜索可获得
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> artistTopSongs({required int id}) {
    return PlayerRequest(
      url: 'https://music.163.com/api/artist/top/song',
      data: {'id': id},
      options: PlayerRequestOptions(method: PlayerRequestMethod.post, crypto: EncryptType.weapi),
    ).request();
  }

  /// 歌手单曲
  /// [id] 歌手 id, 可由搜索接口获得
  /// [PlayerRequestResponse] 返回PlayerRequestResponse请求数据
  Future<PlayerRequestResponse> artists({required int id}) {
    return PlayerRequest(
      url: 'https://music.163.com/weapi/v1/artist/${id.toString()}',
      options: PlayerRequestOptions(
        method: PlayerRequestMethod.post,
        crypto: EncryptType.weapi,
      ),
    ).request();
  }
}